package com.answer.scoring;

import cn.hutool.json.JSONUtil;
import com.answer.model.dto.question.QuestionContentDTO;
import com.answer.model.dto.question.QuestionOptionDTO;
import com.answer.model.entity.Question;
import com.answer.model.entity.ScoringResult;
import com.answer.model.entity.UserAnswer;
import com.answer.model.enums.AppTypeEnum;
import com.answer.model.enums.QuestionContentTypeEnum;
import com.answer.model.enums.ScoreStrategyEnum;
import com.answer.model.vo.QuestionVo;
import com.answer.service.QuestionService;
import com.answer.service.ScoringResultService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 等分类
 */

@ScoringStrategyConfig(appType = 0, scoreType = 0)
public class ScoreScoringStrategyImpl implements ScoringStrategy {

    @Resource
    private QuestionService questionService;

    @Resource
    private ScoringResultService scoringResultService;

    @Override
    public UserAnswer doScore(List<String> choices, Question question) {

        // 获取题目
        QuestionVo questionVo = QuestionVo.objToVo(question);
        List<QuestionContentDTO> questionContentDTO = questionVo.getQuestionContentDTO();
        // 获取评分结果
        Long appId = question.getAppId();
        // 评分
        Double res = getBestQuestionResult(choices, questionContentDTO);
        UserAnswer userAnswer = new UserAnswer();
        userAnswer.setAppId(question.getAppId());
//        userAnswer.setQuestionId(question.getId());
        userAnswer.setAppType(AppTypeEnum.SCORE.getCode());
        userAnswer.setScoringStrategy(ScoreStrategyEnum.CUSTOM.getCode());
        userAnswer.setChoices(JSONUtil.toJsonStr(choices));
        userAnswer.setUserId(1L);
        userAnswer.setResultScore(res);
        return userAnswer;


    }


    private Double getBestQuestionResult(List<String> answerList, List<QuestionContentDTO> questionContentDTO) {
        Map<String, Integer> optionCount = new HashMap<>();

        Double res = 0.0;
        for (QuestionContentDTO question : questionContentDTO) {
            for (String answer : answerList) {
                for (QuestionOptionDTO option : question.getOptions()) {
                    if (option.getKey().equals(answer)) {
                        String result = option.getResult();
                        Double score = option.getScore();
                        res += score;
                    }
                }
            }
        }

        // 新的题目结果评分
        // for (int i = 0; i <= questionContentDTO.size(); i++) {
        //     QuestionContentDTO questionContentDTO1 = questionContentDTO.get(i);
        //     // 选择题
        //     if (Objects.equals(QuestionContentTypeEnum.SELECT.getCode(), questionContentDTO1.getType())) {
        //         if (questionContentDTO1.getKeyResult().equals(answerList.get(i))) {
        //             res = +questionContentDTO1.getScore();
        //         }
        //     }
        //     else if (Objects.equals(QuestionContentTypeEnum.GAP.getCode(), questionContentDTO1.getType())) {
        //         res = res + 1;
        //     }
        // }

        return res;

        // ScoringResult maxScoreResult = questionResults.get(0);
        // for (ScoringResult result : questionResults) {
        //    if (res >= result.getResultScoreRange()){
        //        return result;
        //    }
        // }
    }
}


